package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.OtherImportantResearchAchievementsMapper;
import com.zhijian.medical.entity.po.OtherImportantResearchAchievements;
import com.zhijian.medical.entity.vo.request.OtherImportantResearchAchievementsSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.OtherImportantResearchAchievementsSearchVo;
import com.zhijian.medical.entity.vo.response.OtherImportantResearchAchievementsVo;
import com.zhijian.medical.service.OtherImportantResearchAchievementsService;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 其他重要研究成果业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-05-11 13:30:32
 */
@Slf4j
@Service
public class OtherImportantResearchAchievementsServiceImpl implements OtherImportantResearchAchievementsService {

    @Autowired
    private OtherImportantResearchAchievementsMapper mapper;

    @Override
    public OtherImportantResearchAchievements selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public OtherImportantResearchAchievementsVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(OtherImportantResearchAchievements otherImportantResearchAchievements) {
        Date now = new Date();
        otherImportantResearchAchievements.setCreateTime(now);
        otherImportantResearchAchievements.setUpdateTime(now);
        return mapper.insert(otherImportantResearchAchievements) > 0 ? otherImportantResearchAchievements.getId() : 0L;
    }

    @Override
    public boolean update(OtherImportantResearchAchievements otherImportantResearchAchievements) {
        Date now = new Date();
        otherImportantResearchAchievements.setUpdateTime(now);
        return mapper.updateByPrimaryKey(otherImportantResearchAchievements) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(OtherImportantResearchAchievements record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(OtherImportantResearchAchievementsSaveOrUpdateVo saveOrUpdateVo) {
        OtherImportantResearchAchievements otherImportantResearchAchievements = new OtherImportantResearchAchievements();
        BeanUtils.copyProperties(saveOrUpdateVo, otherImportantResearchAchievements);
        return this.insert(otherImportantResearchAchievements) != 0;
    }

    @Override
    public boolean modify(OtherImportantResearchAchievementsSaveOrUpdateVo saveOrUpdateVo) {
        OtherImportantResearchAchievements old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        OtherImportantResearchAchievements otherImportantResearchAchievements = new OtherImportantResearchAchievements();
        BeanUtils.copyProperties(saveOrUpdateVo, otherImportantResearchAchievements);
        return this.update(otherImportantResearchAchievements);
    }

    @Override
    public OtherImportantResearchAchievementsVo selectOne(OtherImportantResearchAchievementsSearchVo otherImportantResearchAchievementsSearchVo) {
        return mapper.selectOne(otherImportantResearchAchievementsSearchVo);
    }

    @Override
    public Integer count(OtherImportantResearchAchievementsSearchVo otherImportantResearchAchievementsSearchVo) {
        return mapper.count(otherImportantResearchAchievementsSearchVo);
    }

    @Override
    public List<OtherImportantResearchAchievementsVo> selectList(OtherImportantResearchAchievementsSearchVo otherImportantResearchAchievementsSearchVo) {
        return mapper.selectList(otherImportantResearchAchievementsSearchVo);
    }

    @Override
    public Page<OtherImportantResearchAchievementsVo> selectPage(OtherImportantResearchAchievementsSearchVo otherImportantResearchAchievementsSearchVo) {
        AssertUtil.check(Objects.isNull(otherImportantResearchAchievementsSearchVo.getPageNum()) || Objects.isNull(otherImportantResearchAchievementsSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(otherImportantResearchAchievementsSearchVo);
        //分页
        Page<OtherImportantResearchAchievementsVo> page = new Page<>(true, count, otherImportantResearchAchievementsSearchVo.getPageNum(), otherImportantResearchAchievementsSearchVo.getPageSize());
        otherImportantResearchAchievementsSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<OtherImportantResearchAchievementsVo> responseVos = this.selectList(otherImportantResearchAchievementsSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<OtherImportantResearchAchievements> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(OtherImportantResearchAchievementsSearchVo otherImportantResearchAchievementsSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(otherImportantResearchAchievementsSearchVo);
    }

}